<?php

namespace App\Console\Commands;

use App\Game\Game;
use App\Http\Requests\Request;
use App\Models\Conf\Site;
use App\Models\Def\MainGamePlat;
use App\Models\Inf\Player;
use App\Models\Log\PlayerTransfer;
use Illuminate\Console\Command;
use Mockery\Exception;

class TransferCheckCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'transfer:check {--transferIds=} {--sites=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 检查未知状态转账，请求检查订单接口，更正转账状态
     * @return mixed
     */
    public function handle()
    {
        $transfers = $this->unknownTransfer();
        foreach ($transfers as $transfer) {
            $player = $transfer->player;
            if (!empty($player)) {
                $site = $player->site;
            }
            $mainGamePlat = $transfer->plat;

            if (!empty($player) && !empty($site) && !empty($mainGamePlat)) {
                $request = $this->makeRequest($transfer, $mainGamePlat, $player, $site);
                $game = new Game($mainGamePlat->main_game_plat_code);
                try {
                    $game->checkTransfer($request);
                } catch (Exception $exception) {
                    continue;
                }

            }
        }
    }

    /**
     * 获取未知状态转账记录
     */
    public function unknownTransfer()
    {
        $transfer = PlayerTransfer::where('status', PlayerTransfer::TRANSFER_STATUS_UNKNOWN);
        if (!empty($this->option('transferIds'))) {
            $transferIds = explode(',', $this->option('transferIds'));
            $transfer = $transfer->where('transferid', $transferIds)->get();
        }

        if (!empty($this->option('sites'))) {
            $transferIds = explode(',', $this->option('sites'));
            $transfer = $transfer->where('transferid', $transferIds)->get();
        }
        $transfers = $transfer->get();
        return $transfers;
    }


    /**
     * 构建请求
     * @param PlayerTransfer $transfer
     * @param MainGamePlat $mainGamePlat
     * @param Player $player
     * @param Site $site
     * @return Request
     */
    private function makeRequest(PlayerTransfer $transfer, MainGamePlat $mainGamePlat, Player $player, Site $site)
    {
        $request = new Request();
        $transferId = is_null($transfer->plattransferid) ? $transfer->plattransferid : $transfer->transferid;
        $request->offsetSet('mainGamePlat', $mainGamePlat->main_game_plat_code);
        $request->offsetSet('transferid', $transferId);
        $request->offsetSet('accountUserName', $player->username);
        $request->offsetSet('username', $site->username);
        return $request;
    }
}
